//
//  UserGuideBackgroundView.m
//  etionUI
//
//  Created by WangJian on 14-2-24.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "UserGuideBackgroundView.h"

@interface CUserGuideBackgroundView()

@property (nonatomic, retain) NSMutableArray *transparencyAreas;

@end

@implementation CUserGuideBackgroundView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.transparencyAreas = [NSMutableArray array];
        self.opaque = NO;
        self.alpha = .5;
    }
    return self;
}

- (void)dealloc
{
    [_transparencyAreas release];
    [super dealloc];
}

- (void)didMoveToSuperview
{
    if (self.superview != nil)
    {
        self.alpha = 0.0;
        [UIView animateWithDuration:1.0 animations:^{
            self.alpha = 0.5;
        }];
    }
}

- (void)addTransparencyAreaWithFrame:(CGRect)frame
{
    [self.transparencyAreas addObject:[NSValue valueWithCGRect:frame]];
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextAddRect(context, self.bounds);
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextFillPath(context);

    for (NSValue *value in self.transparencyAreas)
    {
        CGContextAddEllipseInRect(context, [value CGRectValue]);
    }
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillPath(context);
}


@end
